home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / wingfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  7.8 KB  |  345 lines

  1. /* cat > headers/wingfx.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* wingfx.h: header for wingfx.c file            */
  4. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  5. /* SCCS information: %W%    %G% - NCSA */
  6.  
  7. #define wingfx.h    1
  8.  
  9. #include "all.h"
  10. #include "newext.h"
  11.  
  12.     static Pixwin *pw;
  13.     static int   width, height;
  14.     static float h_scalex, h_scaley, h_basex, h_basey;
  15.     static float w_scalex, w_scaley, w_basex, w_basey;
  16.     static float i_scalex, i_scaley, i_basex, i_basey;
  17.     static Raster last;
  18.     static Clipper c2d;
  19.  
  20.     static int clipt ();
  21.  
  22. /* EOF */
  23. /* cat > src+obj/wingfx/clip2d.c << "EOF" */
  24. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  25. /* clip2d: clip 2-D                    */
  26. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  27. /* SCCS information: %W%    %G% - NCSA */
  28.  
  29. /* #include "wingfx.h" */
  30.  
  31. clip2d (fx, fy, tx, ty, clp)
  32.     int            *fx, *fy, *tx, *ty;
  33.     Clipper        *clp;
  34. {
  35.     float           t0, t1;
  36.     float           deltax, deltay;
  37.     float           lx0, ly0;
  38.     float           lx1, ly1;
  39.     int             isok = FALSE;
  40.  
  41.     lx0 = (float) *fx;
  42.     ly0 = (float) *fy;
  43.     lx1 = (float) *tx;
  44.     ly1 = (float) *ty;
  45.  
  46.     t0 = 0.0;
  47.     t1 = 1.0;
  48.     deltax = lx1 - lx0;
  49.     if (clipt (-deltax, lx0 - (clp->clip_xmin), &t0, &t1) &&
  50.         clipt (deltax, (clp->clip_xmax) - lx0, &t0, &t1))
  51.     {
  52.  
  53.         deltay = ly1 - ly0;
  54.  
  55.         if (clipt (-deltay, ly0 - (clp->clip_ymin), &t0, &t1) &&
  56.             clipt (deltay, (clp->clip_ymax) - ly0, &t0, &t1))
  57.         {
  58.  
  59.             isok = TRUE;
  60.  
  61.             if (t1 < 1.0)
  62.             {
  63.                 lx1 = lx0 + t1 * deltax;
  64.                 ly1 = ly0 + t1 * deltay;
  65.             }
  66.  
  67.             if (t0 > 0.0)
  68.             {
  69.                 lx0 = lx0 + t0 * deltax;
  70.                 ly0 = ly0 + t0 * deltay;
  71.             }
  72.         }
  73.     }
  74.  
  75.     *fx = ROUND (lx0);
  76.     *fy = ROUND (ly0);
  77.     *tx = ROUND (lx1);
  78.     *ty = ROUND (ly1);
  79.     return (isok);
  80. }
  81. /* EOF */
  82. /* cat > src+obj/wingfx/clipt.c << "EOF" */
  83. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  84. /* clipt: (static) Line clipping algorithms as taken    */
  85. /*      from ACM TOG Vol 3 No 1. The bounding area    */
  86. /*      for the X-Y plane is defined by the points    */
  87. /*      (clip_xmin, clip_ymin) and            */
  88. /*      (clip_xmax, clip_ymax). For the 3-D case, the    */
  89. /*      hither plane corresponds to clip_zmin and     */
  90. /*      the yon plan corresponds to clip_zmax        */
  91. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  92. /* SCCS information: %W%    %G% - NCSA */
  93.  
  94. /* #include "wingfx.h" */
  95.  
  96. static int
  97. clipt (p, q, t0, t1)
  98.     float           p, q;
  99.     float          *t0, *t1;
  100. {
  101.     float           r;
  102.  
  103.     if (p < 0.0)
  104.     {
  105.         r = q / p;
  106.         if (r > *t1)
  107.             return (FALSE);
  108.         else if (r > *t0)
  109.             *t0 = r;
  110.     }
  111.     else if (p > 0.0)
  112.     {
  113.         r = q / p;
  114.         if (r < *t0)
  115.             return (FALSE);
  116.         else if (r < *t1)
  117.             *t1 = r;
  118.     }
  119.     else if (q < 0.0)
  120.         return (FALSE);
  121.  
  122.     return (TRUE);
  123. }
  124. /* EOF */
  125. /* cat > src+obj/wingfx/gfx_init.c << "EOF" */
  126. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  127. /* gfx_init: gfx initialization                */
  128. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  129. /* SCCS information: %W%    %G% - NCSA */
  130.  
  131. /* #include "wingfx.h" */
  132.  
  133. void 
  134. gfx_init ()
  135. {
  136.     pw = (Pixwin *) canvas_pixwin (pcanvas);
  137.     width = (int) window_get (pcanvas, WIN_WIDTH);
  138.     height = (int) window_get (pcanvas, WIN_HEIGHT);
  139.  
  140.     c2d.clip_xmin = 0;
  141.     c2d.clip_xmax = width;
  142.     c2d.clip_ymin = 0;
  143.     c2d.clip_ymax = height;
  144. }
  145. /* EOF */
  146. /* cat > src+obj/wingfx/gfx_line.c << "EOF" */
  147. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  148. /* gfx_line:  gfx line                    */
  149. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  150. /* SCCS information: %W%    %G% - NCSA */
  151.  
  152. /* #include "wingfx.h" */
  153.  
  154. void 
  155. gfx_line (fx, fy, tx, ty, flag)
  156.     float           fx, fy, tx, ty;
  157.     int             flag;
  158. {
  159.     gfx_move (fx, fy);
  160.     gfx_vector (tx, ty, flag);
  161. }
  162. /* EOF */
  163. /* cat > src+obj/wingfx/gfx_move.c << "EOF" */
  164. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  165. /* gfx_move: gfx move                    */
  166. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  167. /* SCCS information: %W%    %G% - NCSA */
  168.  
  169. /* #include "wingfx.h" */
  170.  
  171. void 
  172. gfx_move (tx, ty)
  173.     float           tx, ty;
  174. {
  175.     tx = h_scalex * tx + h_basex;
  176.     last.x = ROUND (tx);
  177.     ty = h_scaley * ty + h_basey;
  178.     last.y = ROUND (ty);
  179. }
  180. /* EOF */
  181. /* cat > src+obj/wingfx/gfx_vector.c << "EOF" */
  182. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  183. /* gfx_vector: gfx vector                */
  184. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  185. /* SCCS information: %W%    %G% - NCSA */
  186.  
  187. /* #include "wingfx.h" */
  188.  
  189. void 
  190. gfx_vector (tx, ty, flag)
  191.     float           tx, ty;
  192.     int             flag;
  193. {
  194.     Raster          sf, lf;
  195.  
  196.     sf = last;
  197.     gfx_move (tx, ty);
  198.     lf = last;
  199.  
  200.     if (flag)
  201.         draw_hline (sf.x, sf.y, lf.x, lf.y);
  202.     else
  203.         plot_line (sf.x, sf.y, lf.x, lf.y);
  204. }
  205. /* EOF */
  206. /* cat > src+obj/wingfx/hid_scale.c << "EOF" */
  207. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  208. /* hid_scale: scaling                    */
  209. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  210. /* SCCS information: %W%    %G% - NCSA */
  211.  
  212. /* #include "wingfx.h" */
  213.  
  214. void 
  215. hid_scale (minx, maxx, miny, maxy)
  216.     float           minx, maxx, miny, maxy;    /* box boundaries */
  217. {
  218.         /* Mapped to a 2-D space with PRECISION pixels horizontally. */
  219.     h_scalex = (float) PRECISION / (maxx - minx);
  220.     h_scaley = h_scalex;
  221.     h_basex = -(h_scalex * minx);
  222.     h_basey = -(h_scaley * miny);
  223. }
  224. /* EOF */
  225. /* cat > src+obj/wingfx/plot_line.c << "EOF" */
  226. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  227. /* plot_line: plot line                    */
  228. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  229. /* SCCS information: %W%    %G% - NCSA */
  230.  
  231. /* #include "wingfx.h" */
  232.  
  233. void 
  234. plot_line (x1, y1, x2, y2)
  235.     int             x1, y1, x2, y2;
  236. {
  237.     Point           f, t;
  238.     float           xf, yf, xt, yt;
  239.  
  240.     if (graph_mode == PLOT_3D)
  241.     {
  242.         f.x = (float) x1 - h_basex;
  243.         f.y = (float) y1 - h_basey;
  244.         t.x = (float) x2 - h_basex;
  245.         t.y = (float) y2 - h_basey;
  246.         zform (&f);
  247.         zform (&t);
  248.         f.x += h_basex;
  249.         f.y += h_basey;
  250.         t.x += h_basex;
  251.         t.y += h_basey;
  252.     }
  253.     else
  254.     {
  255.         f.x = x1;
  256.         f.y = y1;
  257.         t.x = x2;
  258.         t.y = y2;
  259.     }
  260.  
  261.     xf = i_scalex * f.x + i_basex;
  262.     yf = i_scaley * f.y + i_basey;
  263.     xt = i_scalex * t.x + i_basex;
  264.     yt = i_scaley * t.y + i_basey;
  265.  
  266.     f.x = w_scalex * f.x + w_basex;
  267.     f.y = w_scaley * f.y + w_basey;
  268.     t.x = w_scalex * t.x + w_basex;
  269.     t.y = w_scaley * t.y + w_basey;
  270.  
  271.     x1 = ROUND (f.x);
  272.     y1 = ROUND (f.y);
  273.     x2 = ROUND (t.x);
  274.     y2 = ROUND (t.y);
  275.  
  276.     if (x1 >= width || x1 <= 0 ||
  277.         y1 >= height || y1 <= 0 ||
  278.         x2 >= width || x2 <= 0 ||
  279.         y2 >= height || y2 <= 0)
  280.     {
  281.  
  282.         if (!clip2d (&x1, &y1, &x2, &y2, &c2d))
  283.             return;
  284.     }
  285.  
  286.     if (x1 == x2 && y1 == y2)
  287.         return;
  288.     pw_vector (pw, x1, y1, x2, y2, PIX_SRC, 255);
  289.     lw_line (xf, yf, xt, yt);
  290. }
  291. /* EOF */
  292. /* cat > src+obj/wingfx/win_scale.c << "EOF" */
  293. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  294. /* win_scale: window scaling                */
  295. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  296. /* SCCS information: %W%    %G% - NCSA */
  297.  
  298. /* #include "wingfx.h" */
  299.  
  300. void 
  301. win_scale (minx, maxx, miny, maxy)
  302.     float           minx, maxx, miny, maxy;    /* box boundaries */
  303. {
  304.         /* Mapped to the real window 2-D space. */
  305.     minx = h_scalex * minx + h_basex;
  306.     maxx = h_scalex * maxx + h_basex;
  307.     miny = h_scaley * miny + h_basey;
  308.     maxy = h_scaley * maxy + h_basey;
  309.  
  310.     w_scalex = (float) width / (maxx - minx);
  311.     w_scaley = (float) height / (maxy - miny);
  312.  
  313.     if (w_scalex < w_scaley)
  314.     {
  315.         w_scaley = w_scalex;
  316.         w_basex = -w_scalex * minx;
  317.         w_basey = -w_scaley * miny + (height - w_scaley * (maxy - miny)) * 0.5;
  318.     }
  319.     else
  320.     {
  321.         w_scalex = w_scaley;
  322.         w_basey = -w_scaley * miny;
  323.         w_basex = -w_scalex * minx + (width - w_scalex * (maxx - minx)) * 0.5;
  324.     }
  325.  
  326.     i_scalex = (LW_WIDTH * 0.8) / (maxx - minx);
  327.     i_scaley = (LW_HEIGHT * 0.8) / (maxy - miny);
  328.  
  329.     if (i_scalex < i_scaley)
  330.     {
  331.         i_scaley = i_scalex;
  332.         i_basex = 0.1 * LW_WIDTH - i_scalex * minx;
  333.         i_basey = 0.1 * LW_HEIGHT - i_scaley * miny
  334.             + (0.8 * LW_HEIGHT - i_scaley * (maxy - miny)) * 0.5;
  335.     }
  336.     else
  337.     {
  338.         i_scalex = i_scaley;
  339.         i_basey = 0.1 * LW_HEIGHT - i_scaley * miny;
  340.         i_basex = 0.1 * LW_WIDTH - i_scalex * minx
  341.             + (0.8 * LW_WIDTH - i_scalex * (maxx - minx)) * 0.5;
  342.     }
  343. }
  344. /* EOF */
  345.